package fr.ul.ade.prime.ui;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import fr.ul.ade.R;
import fr.ul.ade.prime.model.AdeWebOptions;
import fr.ul.ade.prime.model.Lesson;
import fr.ul.ade.prime.model.Lessons;
import fr.ul.ade.prime.model.Schedule;
import fr.ul.ade.prime.model.ScheduleState;
import fr.ul.ade.prime.service.AdeWebNotificationManager;

/**
 * @author Schnell Michaël
 * @version 1.1
 */
public class ViewSchedule extends AdeWebFragmentActivity implements
		BundleExtraNames, OnCancelListener {

	@SuppressWarnings("hiding")
	protected static final String TAG = ViewSchedule.class.getSimpleName();

	private static final int REFRESH_ID = 1;
	private static final int EDIT_ID = 2;
	private static final int HELP_ID = 3;

	protected Schedule displayedSchedule;

	protected boolean waitingForUpdate;

	protected Lessons displayedLessons;

	protected Lesson flaggedLesson;

	protected boolean handleDialog;

	protected ScheduleUpdateDialog updateDialog;

	// --------

	protected long scheduleId;

	protected String scheduleName;

	protected int schedulePeriod;

	// protected LessonList.ListLessonAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = this.getIntent().getExtras();
		this.scheduleId = bundle.getLong(EXTRA_SCHEDULE_ID);
		this.scheduleName = bundle.getString(EXTRA_SCHEDULE_NAME);
		this.schedulePeriod = bundle.getInt(EXTRA_SCHEDULE_PERIOD);

		// On enlève les notifications (s'il y en a)
		AdeWebNotificationManager
				.removeNotificationForImportantScheduleChange(this,
						AdeWebNotificationManager.SCHEDULE_NOTIFICATION_TAG);

		// Fixe le thème de la page
		this.setTheme(AdeWebOptions.getThemeAde(this));
		// Fixe le titre de la page
		this.setTitle(this.scheduleName);
		// Fixe le sous-titre de la page
		this.getSupportActionBar().setSubtitle(R.string.ViewSchedule_subtitle);
		// Ajoute une petite flèche à gauche du logo dans la barre d'action
		this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		this.initialize();
	}// onCreate(Bundle)

	// /**
	// * {@inheritDoc}
	// * @see android.app.Activity#onStart()
	// */
	// @Override
	// protected void onStart() {
	// super.onStart() ;
	// this.handleDialog = true ;
	//
	// ScheduleState currentState = this.displayedSchedule.getState() ;
	// if (currentState.equals(ScheduleState.UPDATING)) {
	// this.displayUpdateDialog(currentState) ;
	// }// if
	// }// onStart()
	//
	// /**
	// * {@inheritDoc}
	// * @see com.actionbarsherlock.app.SherlockActivity#onStop()
	// */
	// @Override
	// protected void onStop() {
	// super.onStop() ;
	// this.handleDialog = false ;
	// }// onStop()
	//
	// /**
	// * {@inheritDoc}
	// * @see android.support.v4.app.FragmentActivity#onBackPressed()
	// */
	// @Override
	// public void onBackPressed() {
	// super.onBackPressed() ;
	// this.updateDialog = null ;
	// }// onBackPressed()

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Intent userManualIntent = new Intent(this, UserManual.class);

		menu.add(Menu.NONE, REFRESH_ID, Menu.NONE, R.string.ActionBar_refresh)
				.setIcon(R.drawable.ic_action_refresh_holo_dark)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		menu.add(Menu.NONE, HELP_ID, Menu.NONE, R.string.ActionBar_help)
				.setIcon(R.drawable.ic_action_help_holo_light)
				.setIntent(userManualIntent)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

		menu.add(Menu.NONE, EDIT_ID, Menu.NONE, R.string.ActionBar_modify)
				.setIcon(R.drawable.ic_action_edit_holo_dark)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		return super.onCreateOptionsMenu(menu);
	}// onCreateOptionsMenu(Menu)

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case HELP_ID:
			Intent help = new Intent(this, UserManual.class);
			this.startActivity(help);
			return true;

		case EDIT_ID:
			Intent editScheduleIntent = new Intent(this, EditSchedule.class);
			editScheduleIntent.putExtra(EXTRA_SCHEDULE_ID, this.scheduleId);
			this.startActivity(editScheduleIntent);
			return true;

		case REFRESH_ID:
			if (!this.waitingForUpdate
					&& AdeWebOptions.networkIsAvailable(this)) {
				this.service.launchScheduleUpdate(this.scheduleId);
			} else {// No network is available, no need to try updating
				Toast.makeText(this,
						R.string.Sync_no_network,
						Toast.LENGTH_SHORT).show();
			}// if
			return true;

		default /* android.R.id.home */:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}// switch
	}// onOptionsItemSelected(MenuItem)

	private void initialize() {
		this.setContentView(R.layout.view_schedule);

		// this.fragmentAdapter =
		// new FragmentAdapter(this.getSupportFragmentManager()) ;
		//
		// this.pager = (ViewPager)
		// this.findViewById(R.id.view_schedule_viewpager) ;
		// this.pager.setAdapter(this.fragmentAdapter) ;
		// this.pager.setPageMarginDrawable(R.drawable.grey_border_inset_lr);
		// this.pager.setPageMargin(getResources()
		// .getDimensionPixelSize(R.dimen.page_margin_width));
		// this.pager.setCurrentItem(DEFAULT_VIEW_TYPE) ;
	}// initialize()

	protected void displayUpdateDialog(ScheduleState state) {
		// TODO Handle new schedule state
		// if (this.handleDialog) {
		// if (this.updateDialog == null) {
		// this.updateDialog = new ScheduleUpdateDialog(this,
		// R.string.UpdateDialog_title, state.step);
		// } else {
		// this.updateDialog.updateMaxProgress(state.step);
		// }// if
		// }// if
	}// displayUpdateDialog()

	/**
	 * Handles the cancel action.
	 * 
	 * @param actualView
	 *            view of the component who has received a click event
	 */
	@Override
	public void onCancel(DialogInterface dialog) {
		dialog.dismiss();
		this.onBackPressed();
	}// onCancel(DialogInterface)

	protected void dropUpdateDialog() {
		this.updateDialog.dismiss();
		this.updateDialog = null;
	}// dropUpdateDialog()

	public long getScheduleId() {
		return this.scheduleId;
	}

	public String getScheduleName() {
		return this.scheduleName;
	}

	public int getSchedulePeriod() {
		return this.schedulePeriod;
	}

}// class ViewSchedule